/*
日志落地基类实现
    1. 抽象落地基类
    2. 派生子类
    3. 使用工厂模式进行创建与表示分离
*/
#ifndef __M_SINK_M__
#define __M_SINK_M__
#include "message.hpp"
#include "level.hpp"
#include "format.hpp"
#include <fstream>
#include "util.hpp"
namespace log
{
    class LogSink
    {
        public:
        using ptr = std::shared_ptr<LogSink>;
        virtual void log(const char* data,size_t len) =0;
        virtual ~LogSink()
        {

        }
    };
    //落地方向: 标准输出
    class StdoutSink :public LogSink
    {
        public:
        void log(const char* data,size_t len) override
        {
            std::string str =data;
            std::cout << str.substr(0,len) << std::endl;
        } 
    };
    //落地方向: 指定文件
    class FileSink :public LogSink
    {
        public:
        //构造是打开文件明，并打开文件，并且将操作句柄管理起来
        FileSink(const std::string& pathname)
        {
            //创建日志所需的目录
            util::file::createDirectory(util::file::path(pathname));
            //2.创建并开打日志文件
            _ofs.open(pathname,std::ios::binary | std::ios::app);
            assert(_ofs.is_open());
        }
        void log(const char* data,size_t len) override
        {
            _ofs.write(data,len);
            assert(_ofs.good());
        }
        private:
        std::string _pathname;
        std::ofstream _ofs;
    };
    //落地方向: 滚动文件
    class RollSinkBySize :public LogSink
    {
        public:
        RollSinkBySize(const std::string& basename,size_t max_size)
        :_basename(basename),
        _max_fsize(max_size),
        _cur_fsize(0) 
        {
            //创建日志所需的目录
            util::file::createDirectory(util::file::path(_basename));
            //2.创建并开打日志文件
            std::string filename = createNewFileName();
            _ofs.open(filename,std::ios::binary | std::ios::app);
            assert(_ofs.is_open());
        }
        void log(const char* data,size_t len) override
        {
            if(_cur_fsize > _max_fsize)
            {
                _ofs.close();
                std::string filename = createNewFileName();
                _ofs.open(filename,std::ios::binary | std::ios::app);
                assert(_ofs.is_open());
            }
            _ofs.write(data,len);
            assert(_ofs.good());
        }
        private:
        std::string createNewFileName()
        {
            //获取系统时间，以时间来构造文件名扩展名;
            std::stringstream ss;
            time_t t = util::Date::now();
            struct tm lt;
            localtime_r(&t,&lt);
            ss << _basename;
            ss << lt.tm_year+1900;
            ss << lt.tm_mon+1;
            ss << lt.tm_mday;
            ss << lt.tm_hour;
            ss << lt.tm_min;
            ss << lt.tm_sec;
            ss << ".log"; 

            return ss.str();
        }
        std::string _basename;
        std::ofstream _ofs;
        size_t _max_fsize; //记录最大大小，当前文件超过了这个大小就要换文件
        size_t _cur_fsize; //记录当前文件已经写入的数据大小
    };

    class SinkFactory
    {
        public:
        template<class Sinktype,class ...Args>
        static LogSink::ptr Create(Args&& ...args)
        {
            return std::make_shared<Sinktype>(std::forward<Args>(args)...);
        }
    };
}
#endif